package com.resou.reader.reader.manager;

import android.text.TextUtils;

import com.resou.reader.api.entry.ChapterItemBean;
import com.resou.reader.utils.log.DLog;
import com.resouxs.free_book.service.SimilarityUtils;
import com.resouxs.free_book.service.ZsChapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${Garrett} on 2018/9/8.
 * Contact me krouky@outlook.com
 */
public class ReaderManager {
    private static final String TAG = "ReaderManager";

    public static int convertZsChapterToYwChapter(List<ZsChapter> chapters, List<ChapterItemBean> mChapterList, String zsBookId, String curChapterId) {
        if (mChapterList == null) {
            return 0;
        }
        List<ChapterItemBean> zsChapterList = new ArrayList<>();
        int j = 0;
        int tryCnt = 3;
        boolean doNotCompareAnymore = false;
        int revisedIndex = 0;
        for (int i = 0; i < mChapterList.size(); i++) {
            ChapterItemBean chapterItemBean = mChapterList.get(i);
            if (curChapterId != null && chapterItemBean.getChapterId().equals(curChapterId)) {
                revisedIndex = zsChapterList.size();
//                DLog.e(TAG, "revisedIndex: " + revisedIndex);
            }

            if (!doNotCompareAnymore && j < chapters.size() - 1) {
                ZsChapter zsChapter = chapters.get(j);
                if (TextUtils.isEmpty(zsChapter.getLink())) {
                    i--;//不跳过阅文
                    j++;//跳过没有下载链接的追书章节目录
                    continue;
                }
                String zsChapterTitle = zsChapter.getTitle();
                String ywChapterTitle = chapterItemBean.getChapterName();
//                DLog.e(TAG, "zsChapterTitle: " + zsChapterTitle + " ywChapterTitle: " + ywChapterTitle +" "+ zsChapter.getLink());

                zsChapterTitle = subChapterName(zsChapterTitle).trim();
                ywChapterTitle = subChapterName(ywChapterTitle).trim();

                j++;
                float levenshtein = SimilarityUtils.levenshtein(zsChapterTitle, ywChapterTitle);
//                DLog.d(TAG, "levenshtein: "+levenshtein);

                if (levenshtein > 0.35 || zsChapterTitle.contains(ywChapterTitle) || ywChapterTitle.contains(zsChapterTitle)) {
                    tryCnt = 3; //找到章节重置重试状态
                    chapterItemBean.setZsChapterLink(zsChapter.getLink());
                    chapterItemBean.setZsBookId(zsBookId);
                    zsChapterList.add(chapterItemBean);
                } else {
                    //与当前阅文目录的上一章比对
                    zsChapterTitle = zsChapterTitle.replaceFirst("(.*)章(\\s)", "");
                    int indexFoundToCurrentDistance = compareWithYwChapter(zsChapterTitle, mChapterList, i, 10);
//                    DLog.e(TAG, "indexFoundToCurrentDistance: "+indexFoundToCurrentDistance);
                    if (indexFoundToCurrentDistance == 0) {
                        if (tryCnt == 0) {
                            doNotCompareAnymore = true;
                        }
                        tryCnt--;
                        i--;//阅文章节不改变
                    } else if (indexFoundToCurrentDistance < 0) {
                        //1. 如果追书把阅文一个章节分为多个章节
                        //2. 如果追书章节多一个章节， 阅文少一个章节
                        //3. 如果追书某个章节没有匹配上， 但是下一个章节可以匹配上阅文的章节
                        tryCnt = 3;
//                        ChapterItemBean ywChItembean = mChapterList.get(i + indexFoundToCurrentDistance);
//                        ChapterItemBean chapterItemBean2 = new ChapterItemBean();
//                        ChapterItemBean ywChItemBean = ywChItembean;
//                        if(i+indexFoundToCurrentDistance == 0)
//                            chapterItemBean2.setChapterName(ywChItemBean.getChapterName());
//                        else
//                            chapterItemBean2.setChapterName(zsChapterTitle);
//
//                        chapterItemBean2.setZsChapterLink(zsChapter.getLink());
//                        chapterItemBean2.setChapterId(ywChItemBean.getChapterId());
//                        chapterItemBean2.setIsPay(ywChItemBean.getIsPay());
//                        chapterItemBean2.setIsVip(ywChItemBean.getIsVip());
//                        chapterItemBean2.setZsChapterIdSuffix(String.valueOf(j));
//                        chapterItemBean2.setZsBookId(zsBookId);
//                        chapterItemBean2.setBookName(ywChItemBean.getBookName());
//                        chapterItemBean2.setBookId(ywChItemBean.getBookId());
//                        chapterItemBean2.setChapterIndex(zsChapterList.size() - 1);
//                        chapterItemBean2.setChapterSum(ywChItemBean.getChapterSum());
//                        chapterItemBean2.setDescription(ywChItemBean.getDescription());
//                        chapterItemBean2.setShareUrl(ywChItemBean.getShareUrl());
//                        zsChapterList.add(chapterItemBean2);
                        i--;//阅文章节不改变
                    } else {
                        //在当前追书章节目录对于阅文目录的下面几个章节找到了对应的章节目录
                        tryCnt = 3;
                        for (int index = 0; index <= indexFoundToCurrentDistance; index++) {
                            ChapterItemBean chapterItemBean2 = new ChapterItemBean();
                            ChapterItemBean ywChItemBean = mChapterList.get(i + index);
                            chapterItemBean2.setChapterName(ywChItemBean.getChapterName());
                            if (index == indexFoundToCurrentDistance) {
                                chapterItemBean2.setZsChapterLink(zsChapter.getLink());
                            }
                            chapterItemBean2.setChapterId(ywChItemBean.getChapterId());
                            chapterItemBean2.setIsPay(ywChItemBean.getIsPay());
                            chapterItemBean2.setIsVip(ywChItemBean.getIsVip());
                            chapterItemBean2.setZsBookId(zsBookId);
                            chapterItemBean2.setBookName(ywChItemBean.getBookName());
                            chapterItemBean2.setBookId(ywChItemBean.getBookId());
                            chapterItemBean2.setChapterIndex(zsChapterList.size() - 1);
                            chapterItemBean2.setChapterSum(ywChItemBean.getChapterSum());
                            chapterItemBean2.setDescription(ywChItemBean.getDescription());
                            chapterItemBean2.setShareUrl(ywChItemBean.getShareUrl());
//                            DLog.d(TAG, "add  "+chapterItemBean2.getChapterName() +" link: " + chapterItemBean2.getZsChapterLink());
                            zsChapterList.add(chapterItemBean2);
                        }
                        i += indexFoundToCurrentDistance;//跳过比对过的章节， 到比对正确的章节之后
                    }
                }
            } else {
                zsChapterList.add(chapterItemBean);
            }
        }
        mChapterList.clear();
        mChapterList.addAll(zsChapterList);
        zsChapterList.clear();
        return revisedIndex;
    }


    private static int compareWithYwChapter(String zsChapterTitle, List<ChapterItemBean> mChapterList, int currentIndex, int length) {
        ChapterItemBean chapterItemBean = null;
        int currentIndexOffset; //离当前操作的index的距离
        currentIndexOffset = getCurrentIndexOffset(currentIndex);
        int opIndex = 0;
//        DLog.e(TAG, "currentIndex 0 " + currentIndex);
        for (int i = 0; i < length; i++) {
            if (currentIndexOffset == 0) {
                currentIndexOffset += 1;
                continue;
            }
            opIndex = currentIndex + currentIndexOffset;
//            DLog.e(TAG, "currentIndex 0 " + opIndex +" currentIndexOffset: "+currentIndexOffset);

            if (opIndex >= mChapterList.size()) {
                return 0;//未找到
            }
            chapterItemBean = mChapterList.get(opIndex);
            String ywChapterTitle = chapterItemBean.getChapterName();
            ywChapterTitle = subChapterName(ywChapterTitle);
//            DLog.e(TAG, "第 "+i+"次： zsChapterTitle: " + zsChapterTitle + " YwChapterTitle: " + ywChapterTitle);

            float levenshtein3 = SimilarityUtils.levenshtein(zsChapterTitle.trim(), ywChapterTitle.trim());
//            DLog.d(TAG, "levenshtein: "+levenshtein3);
            if (levenshtein3 > 0.35 || zsChapterTitle.contains(ywChapterTitle) || ywChapterTitle.contains(zsChapterTitle)) {
                return currentIndexOffset;
            } else {
                currentIndexOffset += 1;
                continue;
            }
        }
        return 0;
    }

    private static int getCurrentIndexOffset(int currentIndex) {
        int currentIndexOffset = -4;
        for (int i = 0; i < 4; i++) {
            if (currentIndex == i) {
                currentIndexOffset = -i;
                break;
            }
        }
        return currentIndexOffset;
    }

    private static String subChapterName(String chapterTitle) {
        if (chapterTitle.contains("章")) {
            chapterTitle = chapterTitle.replaceFirst("(.*)章(\\s)", "").replaceAll("\\s{1,}", "");
        }
        return chapterTitle;
    }
}
